Iterator Helper๋ฅผ ์ฌ์ฉํ์ฌ JavaScript ๋ฆฌ์์ค ๊ด๋ฆฌ๋ฅผ ์ต์ ํํ์ธ์. ์ต์ JavaScript ๊ธฐ๋ฅ์ผ๋ก ๊ฐ๋ ฅํ๊ณ ํจ์จ์ ์ธ ์คํธ๋ฆผ ๋ฆฌ์์ค ์์คํ ์ ๊ตฌ์ถํ์ญ์์ค.
JavaScript Iterator Helper ๋ฆฌ์์ค ๊ด๋ฆฌ์: ์คํธ๋ฆผ ๋ฆฌ์์ค ์์คํ
์ต์ JavaScript๋ ๋ฐ์ดํฐ ์คํธ๋ฆผ๊ณผ ๋ฆฌ์์ค๋ฅผ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ๋ ๊ฐ๋ ฅํ ๋๊ตฌ๋ฅผ ์ ๊ณตํฉ๋๋ค. Iterator Helper๋ ๋น๋๊ธฐ ์ดํฐ๋ ์ดํฐ ๋ฐ ์ ๋๋ ์ดํฐ ํจ์์ ๊ฐ์ ๊ธฐ๋ฅ๊ณผ ๊ฒฐํฉํ์ฌ ๊ฐ๋ฐ์๊ฐ ๊ฐ๋ ฅํ๊ณ ํ์ฅ ๊ฐ๋ฅํ ์คํธ๋ฆผ ๋ฆฌ์์ค ์์คํ ์ ๊ตฌ์ถํ ์ ์๋๋ก ํฉ๋๋ค. ์ด ๊ธ์์๋ ์ด๋ฌํ ๊ธฐ๋ฅ์ ํ์ฉํ์ฌ ๋ฆฌ์์ค๋ฅผ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ๊ณ , ์ฑ๋ฅ์ ์ต์ ํํ๋ฉฐ, ์ฝ๋ ๊ฐ๋ ์ฑ์ ํฅ์์ํค๋ ์์คํ ์ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์ดํด๋ด ๋๋ค.
JavaScript์์ ๋ฆฌ์์ค ๊ด๋ฆฌ์ ํ์์ฑ ์ดํด
JavaScript ์ ํ๋ฆฌ์ผ์ด์ , ํนํ ๋๊ท๋ชจ ๋ฐ์ดํฐ์ ๋๋ ์ธ๋ถ API๋ฅผ ๋ค๋ฃจ๋ ๊ฒฝ์ฐ ํจ์จ์ ์ธ ๋ฆฌ์์ค ๊ด๋ฆฌ๊ฐ ์ค์ํฉ๋๋ค. ๊ด๋ฆฌ๋์ง ์๋ ๋ฆฌ์์ค๋ ์ฑ๋ฅ ๋ณ๋ชฉ ํ์, ๋ฉ๋ชจ๋ฆฌ ๋์ ๋ฐ ์ฌ์ฉ์ ๊ฒฝํ ์ ํ๋ก ์ด์ด์ง ์ ์์ต๋๋ค. ๋ฆฌ์์ค ๊ด๋ฆฌ๊ฐ ์ค์ํ ์ผ๋ฐ์ ์ธ ์๋๋ฆฌ์ค๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
- ๋์ฉ๋ ํ์ผ ์ฒ๋ฆฌ: ํนํ ๋ธ๋ผ์ฐ์ ํ๊ฒฝ์์ ๋์ฉ๋ ํ์ผ์ ์ฝ๊ณ ์ฒ๋ฆฌํ๋ ค๋ฉด ๋ฉ์ธ ์ค๋ ๋ ์ฐจ๋จ์ ํผํ๊ธฐ ์ํด ์ ์คํ ๊ด๋ฆฌ๊ฐ ํ์ํฉ๋๋ค.
- API๋ก๋ถํฐ ๋ฐ์ดํฐ ์คํธ๋ฆฌ๋ฐ: ๋๊ท๋ชจ ๋ฐ์ดํฐ์ ์ ๋ฐํํ๋ API๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ๋๋ ํด๋ผ์ด์ธํธ์ ๋ถ๋ด์ ์ฃผ์ง ์๋๋ก ์คํธ๋ฆฌ๋ฐ ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌํด์ผ ํฉ๋๋ค.
- ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ๊ด๋ฆฌ: ์ ํ๋ฆฌ์ผ์ด์ ์ ์๋ต์ฑ๊ณผ ํ์ฅ์ฑ์ ๋ณด์ฅํ๋ ค๋ฉด ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ์ ํจ์จ์ ์ผ๋ก ์ฒ๋ฆฌํ๋ ๊ฒ์ด ํ์์ ์ ๋๋ค.
- ์ด๋ฒคํธ ๊ธฐ๋ฐ ์์คํ : ์ด๋ฒคํธ ์คํธ๋ฆผ์ ๊ด๋ฆฌํ๊ณ ์ด๋ฒคํธ ๋ฆฌ์ค๋๊ฐ ์ ๋๋ก ์ ๋ฆฌ๋๋๋ก ํ๋ ๊ฒ์ ๋ฉ๋ชจ๋ฆฌ ๋์๋ฅผ ๋ฐฉ์งํ๋ ๋ฐ ๋งค์ฐ ์ค์ํฉ๋๋ค.
์ ์ค๊ณ๋ ๋ฆฌ์์ค ๊ด๋ฆฌ ์์คํ ์ ํ์ํ ๋ ๋ฆฌ์์ค๋ฅผ ํ๋ํ๊ณ , ํจ์จ์ ์ผ๋ก ์ฌ์ฉํ๋ฉฐ, ๋ ์ด์ ํ์ํ์ง ์์ ๋ ์ ์ํ๊ฒ ํด์ ๋๋๋ก ๋ณด์ฅํฉ๋๋ค. ์ด๋ ์ ํ๋ฆฌ์ผ์ด์ ์ footprint๋ฅผ ์ต์ํํ๊ณ , ์ฑ๋ฅ์ ํฅ์์ํค๋ฉฐ, ์์ ์ฑ์ ๊ฐ์ ํฉ๋๋ค.
Iterator Helper ์๊ฐ
Array.prototype.values() ๋ฉ์๋๋ผ๊ณ ๋ ์๋ ค์ง Iterator Helper๋ ์ดํฐ๋ฌ๋ธ ๋ฐ์ดํฐ ๊ตฌ์กฐ ์์ ์ ์ํ ๊ฐ๋ ฅํ ๋ฐฉ๋ฒ์ ์ ๊ณตํฉ๋๋ค. ์ด ๋ฉ์๋๋ ์ดํฐ๋ ์ดํฐ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์๋ํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์ ์ธ์ ์ด๊ณ ํจ์จ์ ์ธ ๋ฐฉ์์ผ๋ก ๋ณํ, ํํฐ๋ง ๋ฐ ์๋นํ ์ ์๋๋ก ํฉ๋๋ค. ํ์ฌ Stage 4 ์ ์์ด๋ฉฐ ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ง์๋์ง๋ ์์ง๋ง, ํด๋ฆฌํ ๋๋ Babel๊ณผ ๊ฐ์ ํธ๋์คํ์ผ๋ฌ์ ํจ๊ป ์ฌ์ฉํ ์ ์์ต๋๋ค. ๊ฐ์ฅ ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋๋ Iterator Helper๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
map(): ์ดํฐ๋ ์ดํฐ์ ๊ฐ ์์๋ฅผ ๋ณํํฉ๋๋ค.filter(): ์ฃผ์ด์ง ์กฐ๊ฑด์์ ๋ฐ๋ผ ์์๋ฅผ ํํฐ๋งํฉ๋๋ค.take(): ์ฒซ ๋ฒ์งธ n๊ฐ ์์๋ก ๊ตฌ์ฑ๋ ์ ์ดํฐ๋ ์ดํฐ๋ฅผ ๋ฐํํฉ๋๋ค.drop(): ์ฒซ ๋ฒ์งธ n๊ฐ ์์๋ฅผ ๊ฑด๋๋ด ์ ์ดํฐ๋ ์ดํฐ๋ฅผ ๋ฐํํฉ๋๋ค.reduce(): ์ดํฐ๋ ์ดํฐ์ ๊ฐ์ ๋จ์ผ ๊ฒฐ๊ณผ๋ก ๋์ ํฉ๋๋ค.forEach(): ๊ฐ ์์์ ๋ํด ์ ๊ณต๋ ํจ์๋ฅผ ํ ๋ฒ ์คํํฉ๋๋ค.
Iterator Helper๋ ๋ฐ์ดํฐ๋ฅผ ์ง์ฐ ์ฒ๋ฆฌํ ์ ์๊ธฐ ๋๋ฌธ์ ๋น๋๊ธฐ ๋ฐ์ดํฐ ์คํธ๋ฆผ ์์ ์ ํนํ ์ ์ฉํฉ๋๋ค. ์ด๋ ๋ฐ์ดํฐ๊ฐ ํ์ํ ๋๋ง ์ฒ๋ฆฌ๋จ์ ์๋ฏธํ๋ฉฐ, ํนํ ๋๊ท๋ชจ ๋ฐ์ดํฐ์ ์ ์ฒ๋ฆฌํ ๋ ์ฑ๋ฅ์ ํฌ๊ฒ ํฅ์์ํฌ ์ ์์ต๋๋ค.
Iterator Helper๋ฅผ ์ฌ์ฉํ ์คํธ๋ฆผ ๋ฆฌ์์ค ์์คํ ๊ตฌ์ถ
Iterator Helper๋ฅผ ์ฌ์ฉํ์ฌ ์คํธ๋ฆผ ๋ฆฌ์์ค ์์คํ ์ ๊ตฌ์ถํ๋ ๋ฐฉ๋ฒ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค. ํ์ผ ์คํธ๋ฆผ์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ณ Iterator Helper๋ฅผ ์ฌ์ฉํ์ฌ ์ฒ๋ฆฌํ๋ ๊ธฐ๋ณธ์ ์ธ ์์ ๋ถํฐ ์์ํ๊ฒ ์ต๋๋ค.
์์ : ํ์ผ ์คํธ๋ฆผ ์ฝ๊ธฐ ๋ฐ ์ฒ๋ฆฌ
๋์ฉ๋ ํ์ผ์ ์ฝ๊ณ , ๊ฐ ์ค์ ์ฒ๋ฆฌํ๋ฉฐ, ํน์ ์ ๋ณด๋ฅผ ์ถ์ถํด์ผ ํ๋ ์๋๋ฆฌ์ค๋ฅผ ๊ณ ๋ คํด ๋ณด์ญ์์ค. ๊ธฐ์กด ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ฉด ์ ์ฒด ํ์ผ์ ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋ํ ์ ์์ผ๋ฉฐ, ์ด๋ ๋นํจ์จ์ ์ผ ์ ์์ต๋๋ค. Iterator Helper ๋ฐ ๋น๋๊ธฐ ์ดํฐ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ๋ฉด ํ์ผ ์คํธ๋ฆผ์ ํ ์ค์ฉ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค.
๋จผ์ , ํ์ผ ์คํธ๋ฆผ์ ํ ์ค์ฉ ์ฝ๋ ๋น๋๊ธฐ ์ ๋๋ ์ดํฐ ํจ์๋ฅผ ์์ฑํฉ๋๋ค.
async function* readFileLines(filePath) {
const fileStream = fs.createReadStream(filePath, { encoding: 'utf8' });
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
try {
for await (const line of rl) {
yield line;
}
} finally {
// Ensure the file stream is closed, even if errors occur
fileStream.destroy();
}
}
์ด ํจ์๋ Node.js์ fs ๋ฐ readline ๋ชจ๋์ ์ฌ์ฉํ์ฌ ์ฝ๊ธฐ ์คํธ๋ฆผ์ ์์ฑํ๊ณ ํ์ผ์ ๊ฐ ์ค์ ๋ฐ๋ณตํฉ๋๋ค. finally ๋ธ๋ก์ ์ฝ๊ธฐ ํ๋ก์ธ์ค ์ค์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋๋ผ๋ ํ์ผ ์คํธ๋ฆผ์ด ์ ๋๋ก ๋ซํ๋๋ก ๋ณด์ฅํฉ๋๋ค. ์ด๋ ๋ฆฌ์์ค ๊ด๋ฆฌ์ ์ค์ํ ๋ถ๋ถ์
๋๋ค.
๋ค์์ผ๋ก, Iterator Helper๋ฅผ ์ฌ์ฉํ์ฌ ํ์ผ ์คํธ๋ฆผ์ ์ค์ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค.
async function processFile(filePath) {
const lines = readFileLines(filePath);
// Simulate Iterator Helpers
async function* map(iterable, transform) {
for await (const item of iterable) {
yield transform(item);
}
}
async function* filter(iterable, predicate) {
for await (const item of iterable) {
if (predicate(item)) {
yield item;
}
}
// Using \"Iterator Helpers\" (simulated here)
const processedLines = map(filter(lines, line => line.length > 0), line => line.toUpperCase());
for await (const line of processedLines) {
console.log(line);
}
}
์ด ์์ ์์๋ ๋จผ์ ๋น ์ค์ ํํฐ๋งํ ๋ค์, ๋จ์ ์ค์ ๋๋ฌธ์๋ก ๋ณํํฉ๋๋ค. ์ด๋ฌํ ์๋ฎฌ๋ ์ด์
๋ Iterator Helper ํจ์๋ ์คํธ๋ฆผ์ ์ง์ฐ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค. for await...of ๋ฃจํ๋ ์ฒ๋ฆฌ๋ ์ค์ ์๋นํ๊ณ ์ฝ์์ ๋ก๊น
ํฉ๋๋ค.
์ด ์ ๊ทผ ๋ฐฉ์์ ์ด์
- ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ฑ: ํ์ผ์ด ํ ์ค์ฉ ์ฒ๋ฆฌ๋์ด ํ์ํ ๋ฉ๋ชจ๋ฆฌ ์์ด ์ค์ด๋ญ๋๋ค.
- ์ฑ๋ฅ ํฅ์: ์ง์ฐ ํ๊ฐ๋ฅผ ํตํด ํ์ํ ๋ฐ์ดํฐ๋ง ์ฒ๋ฆฌ๋๋๋ก ํฉ๋๋ค.
- ๋ฆฌ์์ค ์์ ์ฑ:
finally๋ธ๋ก์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋๋ผ๋ ํ์ผ ์คํธ๋ฆผ์ด ์ ๋๋ก ๋ซํ๋๋ก ๋ณด์ฅํฉ๋๋ค. - ๊ฐ๋ ์ฑ: Iterator Helper๋ ๋ณต์กํ ๋ฐ์ดํฐ ๋ณํ์ ์ ์ธ์ ์ผ๋ก ํํํ๋ ๋ฐฉ๋ฒ์ ์ ๊ณตํฉ๋๋ค.
๊ณ ๊ธ ๋ฆฌ์์ค ๊ด๋ฆฌ ๊ธฐ์
๊ธฐ๋ณธ ํ์ผ ์ฒ๋ฆฌ๋ฅผ ๋์ด, Iterator Helper๋ ๋ ๊ณ ๊ธ ๋ฆฌ์์ค ๊ด๋ฆฌ ๊ธฐ์ ์ ๊ตฌํํ๋ ๋ฐ ์ฌ์ฉ๋ ์ ์์ต๋๋ค. ๋ค์์ ๋ช ๊ฐ์ง ์์ ๋๋ค.
1. Rate Limiting
์ธ๋ถ API์ ์ํธ ์์ฉํ ๋, API ์ฌ์ฉ ์ ํ์ ์ด๊ณผํ์ง ์๊ธฐ ์ํด Rate Limiting์ ๊ตฌํํด์ผ ํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค. Iterator Helper๋ฅผ ์ฌ์ฉํ์ฌ API๋ก ์ ์ก๋๋ ์์ฒญ์ ์๋๋ฅผ ์ ์ดํ ์ ์์ต๋๋ค.
async function* rateLimit(iterable, delay) {
for await (const item of iterable) {
yield item;
await new Promise(resolve => setTimeout(resolve, delay));
}
}
async function* fetchFromAPI(urls) {
for (const url of urls) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
yield await response.json();
}
}
async function processAPIResponses(urls, rateLimitDelay) {
const apiResponses = fetchFromAPI(urls);
const rateLimitedResponses = rateLimit(apiResponses, rateLimitDelay);
for await (const response of rateLimitedResponses) {
console.log(response);
}
}
// Example usage:
const apiUrls = [
'https://api.example.com/data1',
'https://api.example.com/data2',
'https://api.example.com/data3'
];
// Set a rate limit of 500ms between requests
await processAPIResponses(apiUrls, 500);
์ด ์์ ์์ rateLimit ํจ์๋ ์ดํฐ๋ฌ๋ธ์์ ๋ฐฉ์ถ๋๋ ๊ฐ ํญ๋ชฉ ์ฌ์ด์ ์ง์ฐ์ ๋์
ํฉ๋๋ค. ์ด๋ API ์์ฒญ์ด ์ ์ด๋ ์๋๋ก ์ ์ก๋๋๋ก ๋ณด์ฅํฉ๋๋ค. fetchFromAPI ํจ์๋ ์ง์ ๋ URL์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ JSON ์๋ต์ yieldํฉ๋๋ค. processAPIResponses๋ ์ด๋ฌํ ํจ์๋ฅผ ๊ฒฐํฉํ์ฌ Rate Limiting์ ํตํด API ์๋ต์ ๊ฐ์ ธ์ค๊ณ ์ฒ๋ฆฌํฉ๋๋ค. ์ ์ ํ ์ค๋ฅ ์ฒ๋ฆฌ(์: response.ok ํ์ธ)๋ ํฌํจ๋ฉ๋๋ค.
2. Resource Pooling
๋ฆฌ์์ค ํ๋ง์ ๋ฆฌ์์ค๋ฅผ ๋ฐ๋ณต์ ์ผ๋ก ์์ฑํ๊ณ ํ๊ดดํ๋ ์ค๋ฒํค๋๋ฅผ ํผํ๊ธฐ ์ํด ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ๋ฆฌ์์ค ํ์ ์์ฑํ๋ ๊ฒ์ ํฌํจํฉ๋๋ค. Iterator Helper๋ฅผ ์ฌ์ฉํ์ฌ ํ์์ ๋ฆฌ์์ค ํ๋ ๋ฐ ํด์ ๋ฅผ ๊ด๋ฆฌํ ์ ์์ต๋๋ค.
์ด ์์ ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ์ ์ํ ๋จ์ํ๋ ๋ฆฌ์์ค ํ์ ๋ณด์ฌ์ค๋๋ค.
class ConnectionPool {
constructor(size, createConnection) {
this.size = size;
this.createConnection = createConnection;
this.pool = [];
this.available = [];
this.initializePool();
}
async initializePool() {
for (let i = 0; i < this.size; i++) {
const connection = await this.createConnection();
this.pool.push(connection);
this.available.push(connection);
}
}
async acquire() {
if (this.available.length > 0) {
return this.available.pop();
}
// Optionally handle the case where no connections are available, e.g., wait or throw an error.
throw new Error(\"No available connections in the pool.\");
}
release(connection) {
this.available.push(connection);
}
async useConnection(callback) {
const connection = await this.acquire();
try {
return await callback(connection);
} finally {
this.release(connection);
}
}
}
// Example Usage (assuming you have a function to create a database connection)
async function createDBConnection() {
// Simulate creating a database connection
return new Promise(resolve => {
setTimeout(() => {
resolve({ id: Math.random(), query: (sql) => Promise.resolve(`Executed: ${sql}`) }); // Simulate a connection object
}, 100);
});
}
async function main() {
const poolSize = 5;
const pool = new ConnectionPool(poolSize, createDBConnection);
// Wait for the pool to initialize
await new Promise(resolve => setTimeout(resolve, 100 * poolSize));
// Use the connection pool to execute queries
for (let i = 0; i < 10; i++) {
try {
const result = await pool.useConnection(async (connection) => {
return await connection.query(`SELECT * FROM users WHERE id = ${i}`);
});
console.log(`Query ${i} Result: ${result}`);
} catch (error) {
console.error(`Error executing query ${i}: ${error.message}`);
}
}
}
main();
์ด ์์ ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ํ์ ๊ด๋ฆฌํ๋ ConnectionPool ํด๋์ค๋ฅผ ์ ์ํฉ๋๋ค. acquire ๋ฉ์๋๋ ํ์์ ์ฐ๊ฒฐ์ ๊ฒ์ํ๊ณ , release ๋ฉ์๋๋ ์ฐ๊ฒฐ์ ํ๋ก ๋ฐํํฉ๋๋ค. useConnection ๋ฉ์๋๋ ์ฐ๊ฒฐ์ ํ๋ํ๊ณ , ์ฐ๊ฒฐ๋ก ์ฝ๋ฐฑ ํจ์๋ฅผ ์คํํ ๋ค์, ์ฐ๊ฒฐ์ ํด์ ํ์ฌ ์ฐ๊ฒฐ์ด ํญ์ ํ๋ก ๋ฐํ๋๋๋ก ๋ณด์ฅํฉ๋๋ค. ์ด ์ ๊ทผ ๋ฐฉ์์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ฆฌ์์ค๋ฅผ ํจ์จ์ ์ผ๋ก ์ฌ์ฉํ๊ณ ์ ์ฐ๊ฒฐ์ ๋ฐ๋ณต์ ์ผ๋ก ์์ฑํ๋ ์ค๋ฒํค๋๋ฅผ ํผํ๋ ๋ฐ ๋์์ด ๋ฉ๋๋ค.
3. Throttling
Throttling์ ์์คํ ๊ณผ๋ถํ๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด ๋์ ์์ ์๋ฅผ ์ ํํฉ๋๋ค. Iterator Helper๋ ๋น๋๊ธฐ ์์ ์ ์คํ์ Throttlingํ๋ ๋ฐ ์ฌ์ฉ๋ ์ ์์ต๋๋ค.
async function* throttle(iterable, concurrency) {
const queue = [];
let running = 0;
let iterator = iterable[Symbol.asyncIterator]();
async function execute() {
if (queue.length === 0 || running >= concurrency) {
return;
}
running++;
const { value, done } = queue.shift();
try {
yield await value;
} finally {
running--;
if (!done) {
execute(); // Continue processing if not done
}
}
if (queue.length > 0) {
execute(); // Start another task if available
}
}
async function fillQueue() {
while (running < concurrency) {
const { value, done } = await iterator.next();
if (done) {
return;
}
queue.push({ value, done });
execute();
}
}
await fillQueue();
}
async function* generateTasks(count) {
for (let i = 1; i <= count; i++) {
yield new Promise(resolve => {
const delay = Math.random() * 1000;
setTimeout(() => {
console.log(`Task ${i} completed after ${delay}ms`);
resolve(`Result from task ${i}`);
}, delay);
});
}
}
async function main() {
const taskCount = 10;
const concurrencyLimit = 3;
const tasks = generateTasks(taskCount);
const throttledTasks = throttle(tasks, concurrencyLimit);
for await (const result of throttledTasks) {
console.log(`Received: ${result}`);
}
console.log('All tasks completed');
}
main();
์ด ์์ ์์ throttle ํจ์๋ ๋์ ๋น๋๊ธฐ ์์
์๋ฅผ ์ ํํฉ๋๋ค. ์ด ํจ์๋ ๋๊ธฐ ์ค์ธ ์์
ํ๋ฅผ ์ ์งํ๊ณ ์ง์ ๋ ๋์์ฑ ์ ํ๊น์ง ์คํํฉ๋๋ค. generateTasks ํจ์๋ ๋ฌด์์ ์ง์ฐ ํ์ ํด๊ฒฐ๋๋ ์ผ๋ จ์ ๋น๋๊ธฐ ์์
์ ์์ฑํฉ๋๋ค. main ํจ์๋ ์ด๋ฌํ ํจ์๋ฅผ ๊ฒฐํฉํ์ฌ Throttling๊ณผ ํจ๊ป ์์
์ ์คํํฉ๋๋ค. ์ด๋ ์์คํ
์ด ๋๋ฌด ๋ง์ ๋์ ์์
์ผ๋ก ๊ณผ๋ถํ๋์ง ์๋๋ก ๋ณด์ฅํฉ๋๋ค.
์ค๋ฅ ์ฒ๋ฆฌ
๊ฐ๋ ฅํ ์ค๋ฅ ์ฒ๋ฆฌ๋ ๋ชจ๋ ๋ฆฌ์์ค ๊ด๋ฆฌ ์์คํ ์ ํ์์ ์ธ ๋ถ๋ถ์ ๋๋ค. ๋น๋๊ธฐ ๋ฐ์ดํฐ ์คํธ๋ฆผ ์์ ์ ๋ฆฌ์์ค ๋์๋ฅผ ๋ฐฉ์งํ๊ณ ์ ํ๋ฆฌ์ผ์ด์ ์์ ์ฑ์ ๋ณด์ฅํ๊ธฐ ์ํด ์ค๋ฅ๋ฅผ ์ฐ์ํ๊ฒ ์ฒ๋ฆฌํ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค. ์ค๋ฅ๊ฐ ๋ฐ์ํ๋๋ผ๋ ๋ฆฌ์์ค๊ฐ ์ ๋๋ก ์ ๋ฆฌ๋๋๋ก try-catch-finally ๋ธ๋ก์ ์ฌ์ฉํ์ญ์์ค.
์๋ฅผ ๋ค์ด, ์์ ์๋ readFileLines ํจ์์์ finally ๋ธ๋ก์ ์ฝ๊ธฐ ํ๋ก์ธ์ค ์ค์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋๋ผ๋ ํ์ผ ์คํธ๋ฆผ์ด ๋ซํ๋๋ก ๋ณด์ฅํฉ๋๋ค.
๊ฒฐ๋ก
JavaScript Iterator Helper๋ ๋น๋๊ธฐ ๋ฐ์ดํฐ ์คํธ๋ฆผ์์ ๋ฆฌ์์ค๋ฅผ ๊ด๋ฆฌํ๋ ๊ฐ๋ ฅํ๊ณ ํจ์จ์ ์ธ ๋ฐฉ๋ฒ์ ์ ๊ณตํฉ๋๋ค. Iterator Helper๋ฅผ ๋น๋๊ธฐ ์ดํฐ๋ ์ดํฐ ๋ฐ ์ ๋๋ ์ดํฐ ํจ์์ ๊ฐ์ ๊ธฐ๋ฅ๊ณผ ๊ฒฐํฉํจ์ผ๋ก์จ ๊ฐ๋ฐ์๋ ๊ฐ๋ ฅํ๊ณ ํ์ฅ ๊ฐ๋ฅํ๋ฉฐ ์ ์ง ๊ด๋ฆฌ ๊ฐ๋ฅํ ์คํธ๋ฆผ ๋ฆฌ์์ค ์์คํ ์ ๊ตฌ์ถํ ์ ์์ต๋๋ค. ์ ์ ํ ๋ฆฌ์์ค ๊ด๋ฆฌ๋ ํนํ ๋๊ท๋ชจ ๋ฐ์ดํฐ์ ๋๋ ์ธ๋ถ API๋ฅผ ๋ค๋ฃจ๋ JavaScript ์ ํ๋ฆฌ์ผ์ด์ ์ ์ฑ๋ฅ, ์์ ์ฑ ๋ฐ ์ ๋ขฐ์ฑ์ ๋ณด์ฅํ๋ ๋ฐ ์ค์ํฉ๋๋ค. Rate Limiting, ๋ฆฌ์์ค ํ๋ง ๋ฐ Throttling๊ณผ ๊ฐ์ ๊ธฐ์ ์ ๊ตฌํํ์ฌ ๋ฆฌ์์ค ์ฌ์ฉ์ ์ต์ ํํ๊ณ , ๋ณ๋ชฉ ํ์์ ๋ฐฉ์งํ๋ฉฐ, ์ ๋ฐ์ ์ธ ์ฌ์ฉ์ ๊ฒฝํ์ ๊ฐ์ ํ ์ ์์ต๋๋ค.